package com.budee.boke.filter;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.i18n.SessionLocaleResolver;

public class LocaleFilter implements Filter {

	public void destroy() {
		

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		String lang ="zh";
		Cookie[] cookies= ((HttpServletRequest)request).getCookies();
		if(cookies != null && cookies.length>0){
			for(Cookie cookie:cookies){
				if("lang".equals(cookie.getName())){
					lang = cookie.getValue();
				}
			}
		}
		System.out.println(lang+"dfffffffffffffffffffffffffdsf");
		 ((HttpServletRequest)request).getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale(lang));
		 chain.doFilter(request, response);
	
	}

	public void init(FilterConfig arg0) throws ServletException {
		

	}

}
